  layui.config({
     base: CXL.ctxPath + '/layuiadmin/' //静态资源所在路径
  }).extend({
    index: 'lib/index' //主入口模块
  }).use(['index', 'form','treetable'], function(){
      var form = layui.form;
      var table = layui.table;
      var treetable = layui.treetable;

      //先隐藏
      $("#carNoDisplay").hide();
      //先隐藏
      $("#spanNoDisplay").hide();


      var taskId;
      // 监听 select 事件
      form.on('select(select-tankCarNo)', function(data){
          var tankCarNo = data.value; // 获得被选中的值
          //taskId重置
          taskId = '';
          Page.getTankCarInfo(tankCarNo);

      });


      // 监听 select 事件
      form.on('select(select-distriTaskType)', function(data){
          var distriTaskType = data.value; // 获得被选中的值
          if(distriTaskType == '1'){
              //先隐藏
              $("#carNoDisplay").hide();
              //先隐藏
              $("#spanNoDisplay").hide();
          } else {
              //先隐藏
              $("#carNoDisplay").show();
              //先隐藏
              $("#spanNoDisplay").show();
          }

      });

      var Page = {
          tableId: 'sys-task-table',
          url: CXL.ctxPath + '/sys/addTask/taskDistribute/getDetailTaskInfo',
          addPageUrl:CXL.ctxPath + '/sys/addTask/openPage',
          editPageUrl:CXL.ctxPath + '/sys/addTask/openEditPage',
          initColumn:function(){
              return [[
                  {type : 'numbers',title : '序号',width:'3%'},
                  {field:'idKey',title:'idKey',hide:true,width:'1%'},
                  {field:'taskId',title:'任务号',width:'6%'},
                  {field:'taskType',title:'任务类型',width:'12%',templet:"#task-type"},
                  {field:'materialName',title:'物料名称',width:'12%'},
                  {field:'materialNo',title:'物料编码',width:'12%'},
                  {field:'status',title:'任务状态',width:'12%',templet:"#online-status"},
                  {field:'targetWeight',title:'目标重量',width:'12%'},
                  {field:'actualWeight',title:'实际重量',width:'12%'},
                  {title:'操作',width:'15%',toolbar: '#sys-task-tool'}
              ]];
          },
      };


      //根据槽车获取对应的任务详情信息
      Page.getTankCarInfo = function (tankCarNo){

          var param = {tankCarNo:tankCarNo};
          treetable.render({
              elem: '#' + Page.tableId
              ,id:'taskTable'
              ,url: CXL.ctxPath + '/sys/addTask/initPrimaryTaskExecution'
              ,height: 'full'
              ,treeColIndex: 2   //树形图标显示在第几列
              ,treeSpid: -1     //最上级的父级id
              ,treeIdName: 'idKey'   //id字段的名称
              ,treePidName: 'fatherId'  //父级节点字段
              ,treeDefaultClose: true   //是否默认折叠
              ,treeLinkage:false  //父级展开时是否自动展开所有子级
              ,page: false
              ,autoSort:false
              ,where:param
              ,defaultToolbar: []
              ,cols: Page.initColumn()
          });
      };


      //增加任务类型
      $("#btnAdd").click(function (){

          var tankCarNo = $('#tankCarNo option:selected').val();
          var tankCarName = $('#tankCarNo option:selected').text();
          if(tankCarNo == null || tankCarNo == ''){
              CXL.warn('请选择对应物料信息的槽车');
              return;
          }
          parent.layer.confirm('确定在'+ tankCarName + '新增一个任务吗？',function(){
              $.ajax({
                  url:CXL.ctxPath + '/sys/addTask/addOpt',
                  method:'POST',
                  data:{tankCarNo:tankCarNo},
                  success:function(res){
                      if(res.success){
                          CXL.success(res.msg);
                          Page.getTankCarInfo(tankCarNo);

                      } else {
                          CXL.warn(res.msg);
                      }
                  }

              })
          });


      });

      Page.refresh = function (taskId){
          //更新该任务的创建时间
          $.ajax({
              url:CXL.ctxPath + '/sys/addTask/refresh',
              method:'POST',
              data:{taskId:taskId},
              success:function(res){
                  if(res.success){
                      CXL.success(res.msg);
                  } else {
                      CXL.warn(res.msg);
                  }
              }

          })

      }


      //增加任务类型
      $("#btnDistribute").click(function (){

          var taskType = $('#taskType option:selected').val();
          var tankCarNo = $('#tankCarNo option:selected').val();
          var spanNo = $('#spanNo option:selected').val();
          var carNo = $('#carNo option:selected').val();

          if(taskType == null || taskType == ''){
              CXL.warn('请选择任务操作类型');
              return;
          }
          var tableData = table.getData(Page.tableId);

          if(tableData == null || tableData.length == 0){
              CXL.warn('当前槽车无任何可执行的任务信息');
              return;
          }
          var taskId = tableData[0].taskId;
          var materialNum = tableData.length;

          Page.distribute(taskType,tankCarNo,taskId,materialNum,spanNo,carNo);
      });

      //编辑任务类型
      table.on('tool(' + Page.tableId + ')', function(obj){
          //获得当前行数据
          var data = obj.data;

          var taskType = $('#taskType option:selected').val();
          var idKey = data.idKey;
          var taskId = data.taskId;
          //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
          var layEvent = obj.event;
          var tankCarNo = $('#tankCarNo option:selected').val();

          //编辑操作
          if(layEvent === 'edit'){
              Page.openEdit(idKey);
          }
          if(layEvent === 'delDetailTask'){
              Page.del(idKey,taskId,tankCarNo);
          }

          if(layEvent === 'refresh'){
              Page.refresh(taskId);
          }


          if(layEvent == 'addMaterial'){
              Page.openAddPage(taskId,null,"I",tankCarNo);
          }
          if(layEvent == 'doTask'){
              var spanNo = $('#spanNo option:selected').val();
              var carNo = $('#carNo option:selected').val();
              var distriTaskType = $('#distriTaskType option:selected').val();
              var dataTaskType = data.taskType;

              if(distriTaskType == null || distriTaskType == ''){
                  CXL.warn('请选择任务下发类型');
                  return;
              }

              if(taskType == null || taskType == ''){
                  CXL.warn('请选择任务操作类型');
                  return;
              }


              if(distriTaskType == '1'){
                  //MES任务下发
                  if(dataTaskType != 'MES'){
                      CXL.warn('该任务的任务类型非MES任务类型！！');
                      return;
                  }
                  layer.confirm("确定要下发此MES任务吗？",function () {
                      $.ajax({
                          type: "post",
                          async:false,
                          url: CXL.ctxPath + '/sys/addTask/checkCarStatus',
                          data:{spanNo:null},
                          success: function(res) {
                              if(res.success){
                                  Page.distributeForMES(taskId,tankCarNo);
                              } else{
                                  CXL.warn(res.msg);
                              }
                          }
                      });
                  })

              } else {

                  if(spanNo == null || spanNo == ''){
                      CXL.warn('请选择工作跨');
                      return;
                  }

                  if(carNo == null || carNo == ''){
                      CXL.warn('请选择天车');
                      return;
                  }
                  if(dataTaskType != 'TQ'){
                      CXL.warn('该任务的任务类型非系统任务类型！！');
                      return;
                  }

                  layer.confirm("确定要下发此系统任务吗？",function () {
                      $.ajax({
                          type: "post",
                          async:false,
                          url: CXL.ctxPath + '/sys/addTask/checkCarStatus',
                          data:{spanNo:spanNo},
                          success: function(res) {
                              if(res.success){
                                  Page.distribute(taskType,tankCarNo,taskId,0,spanNo,carNo);
                              } else{
                                  CXL.warn(res.msg);
                              }
                          }
                      });

                  });


              }

          }

          if(layEvent == 'forceFinishTask'){
              Page.forceFinishTask(taskId);
          }

      });

      Page.forceFinishTask = function (taskId){
          $.ajax({
              type: "post",
              url: CXL.ctxPath + '/sys/addTask/forceFinishTask',
              data:{taskId:taskId},
              success: function(res) {
                  if(res.success) {
                      CXL.success(res.msg);
                  }else {
                      CXL.warn(res.msg);
                  }
              }
          });


      }

      Page.del = function (idKey,taskId,tankCarNo){
          $.ajax({
              type: "post",
              url: CXL.ctxPath + '/sys/addTask/del',
              data:{idKey:idKey,taskId:taskId},
              success: function(res) {
                  if(res.success) {
                      CXL.success(res.msg);
                      Page.getTankCarInfo(tankCarNo);
                  }else {
                      CXL.warn(res.msg);
                  }
              }
          });

      };

      Page.distributeForMES = function (taskId,tankCarNo){
          $.ajax({
              type: "post",
              url: CXL.ctxPath + '/sys/addTask/confirmForMES',
              data:{tankCarNo:tankCarNo,taskId:taskId},
          });
          window.location.reload();
          CXL.success("下发成功");
      };

      Page.distribute = function (taskType,tankCarNo,taskId,materialNum,spanNo,carNo){
          $.ajax({
              type: "post",
              url: CXL.ctxPath + '/sys/addTask/confirm',
              data:{tankCarNo:tankCarNo,oprType:taskType,taskId:taskId,materialNum:materialNum,spanNo:spanNo,carNo:carNo},
          });
          window.location.reload();
          CXL.success("下发成功");
      };


      Page.delPriTask = function (){

      }

      Page.openEdit = function (idKey){
          layer.open({
              type : 2,
              title : '编辑物料',
              shadeClose : true,
              shade : 0.8,
              area: ['600px', '600px'],
              content : Page.editPageUrl + "?idKey=" +idKey ,
              btn : [ '确定', '取消' ],
              yes : function(index, layero) {
                  //点击确认触发 iframe 内容中的按钮提交
                  var submit = layero.find('iframe').contents().find("#form-btn-save");
                  submit.click();

              },
              //层销毁后触发的回调
              end:function (){
                  var tankCarNo = $('#tankCarNo option:selected').val();
                  if(tankCarNo != null && tankCarNo != ''){
                      Page.getTankCarInfo(tankCarNo);
                  }

              }

          });
      };

      Page.checkCarStatus = function  (spanNo){
          $.ajax({
              type: "post",
              async:false,
              url: CXL.ctxPath + '/sys/addTask/checkCarStatus',
              data:{spanNo:spanNo},
              success: function(res) {
                  return res;
              }
          });
      }


      Page.openAddPage = function (taskId,idKey,oprType,tankCarNo){
          //任务号id  子任务主键id   操作类型
          if(taskId == null){
              taskId = "";
          };
          if(idKey == null){
              idKey = "";
          };
          if(oprType == null){
              oprType = "";
          };

          layer.open({
              type : 2,
              title : '增加物料',
              shadeClose : true,
              shade : 0.8,
              area: ['600px', '600px'],
              content : Page.addPageUrl + "?taskId=" + taskId + "&idKey=" + idKey + "&oprType="+oprType + "&tankCarNo=" + tankCarNo,
              btn : [ '确定', '取消' ],
              yes : function(index, layero) {
                  //点击确认触发 iframe 内容中的按钮提交
                  var submit = layero.find('iframe').contents().find("#form-btn-save");
                  submit.click();

              },
              //层销毁后触发的回调
              end:function (){
                  var tankCarNo = $('#tankCarNo option:selected').val();
                  if(tankCarNo != null && tankCarNo != ''){
                      Page.getTankCarInfo(tankCarNo);
                  }

              }
          });

      };




  })